//
//  LinePreTab2Cell.m
//  ImGuider
//
//  Created by 李陆涛 on 2017/9/21.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "LinePreTab2Cell.h"
#import "StarRateView.h"
#import "UserCommentsCell.h"
#import "LinePreTab2FunctionCell.h"
#import "IGAvPlayerManager.h"

@interface LinePreTab2Cell ()
@property (weak, nonatomic) IBOutlet UIView *headView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIView *scoreView;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet StarRateView *starView;

@property (weak, nonatomic) IBOutlet UILabel *totalCountLabel;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeight;

@property (nonatomic, strong) NSMutableDictionary *cellHeightDict;

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic) NSInteger count;

@end

@implementation LinePreTab2Cell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.cellHeightDict = [NSMutableDictionary dictionary];
}

- (void)setType:(NSInteger)type {
    
    _type = type;
    
    if (type == 1) {// 用户点评
        
        self.titleLabel.text = MYLocalizedString(@"SITEPLAY_USER_COMMENT", nil);
        
    } else {
        
        self.titleLabel.text = MYLocalizedString(@"SITEPLAY_USER_QUESTION", nil);
    }
}

- (void)setLinemodel:(LineModel *)linemodel {
    
    _linemodel = linemodel;
    
    
    if (self.type == 1) {
        self.dataArray = linemodel.comments;
        
        [self.starView setScore:self.linemodel.score.floatValue / 10];
        self.scoreLabel.text = [NSString stringWithFormat:@"%0.1f",ceil(self.linemodel.score.floatValue / 10)];
        
        self.totalCountLabel.text = [NSString localizedStringWithFormat:MYLocalizedString(@"COMMENT_COUNT", nil),linemodel.commentnum];
        
        self.count = self.dataArray.count;
        
        if (self.linemodel.commentnum.integerValue > self.dataArray.count) {
            self.count += 1;
        }
        
    } else {
        
        self.dataArray = linemodel.questions;
        
        self.count = self.dataArray.count;
        
        self.totalCountLabel.text = [NSString localizedStringWithFormat:MYLocalizedString(@"ASKED_COUNT", nil),linemodel.questionnum];
        
        if (self.linemodel.questionnum.integerValue > self.dataArray.count) {
            self.count += 1;
        }
    }
    
    if (self.count == 0) {
        
        self.tableViewHeight.constant = self.tableView.tableHeaderView.frame.size.height + 8 + 80;
    }
    
    [self.tableView reloadData];
}


#pragma mark -UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.count == 0 ? 1 : self.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.count == 0) {
        LinePreTab2FunctionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OpenAllCell"];
        cell.functionBtn.hidden = YES;
        
        cell.textLabel.font = Font(14);
        cell.textLabel.textColor = kContentColor;
        
        if (self.type == 1) {
            cell.textLabel.text = MYLocalizedString(@"LINEDETAIL_NO_C", nil);
        } else {
            cell.textLabel.text = MYLocalizedString(@"LINEDETAIL_NO_Q", nil);
        }
        
        return cell;
        
        return cell;
    }
    
    if (indexPath.row >= self.dataArray.count) {
        LinePreTab2FunctionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OpenAllCell"];
        
        [cell.functionBtn addTarget:self action:@selector(showAll:) forControlEvents:UIControlEventTouchUpInside];
        
        if (self.type == 1) {
            
            [cell.functionBtn setTitle:MYLocalizedString(@"LINEDETAIL_SHOW_ALLC", nil) forState:UIControlStateNormal];
        } else {
            
            [cell.functionBtn setTitle:MYLocalizedString(@"LINEDETAIL_SHOW_ALLQ", nil) forState:UIControlStateNormal];
        }
        
        return cell;
    }
    
    if (self.type == 1) {// 用户点评
        
        Comment *comment = self.dataArray[indexPath.row];
        
        UserCommentsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UserCommentsCell"];
        [cell configDataWith:comment];
        
        return cell;
    } else {
        
        Question *question = self.dataArray[indexPath.row];
        question.status = @(3).stringValue;
        UserSubmitQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UserSubmitQuestionCell"];
        cell.superVC = self.superVC;
        cell.delegate = self.delegate;
        cell.question = question;
        
        if ([IGAvPlayerManager shareManeger].isPlay && [[IGAvPlayerManager shareManeger].currentURL isEqualToString:question.answer]) {
            
            [cell startPlay];
        } else {
            
            [cell stopPlay];
        }
        
        return cell;
    }
    
    
}

- (void)showAll:(UIButton *)sender {
    

    if (self.type == 1) {
        [_delegate jumpcomment];
    } else {
        [_delegate jumpquestion];
    }
    
}
#pragma mark -UITableViewDelegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.count == 0) {
        
        return;
    }
    
    if (self.cellHeightDict.count == self.count) {
        return;
    }
    
    NSNumber *height = @(cell.frame.size.height);
    [self.cellHeightDict setObject:height forKey:indexPath];
    
    if (self.cellHeightDict.count == self.count) {
        
        CGFloat tbHeight = 0;
        
        for (NSIndexPath *key in self.cellHeightDict.allKeys) {
            
            NSNumber *height = self.cellHeightDict[key];
            
            tbHeight += height.floatValue;
        }
        
        self.tableViewHeight.constant = tbHeight + self.tableView.tableHeaderView.frame.size.height + 8;
        
        if ([self.delegate respondsToSelector:@selector(reloadTab2Height)]) {
            
            [self.delegate reloadTab2Height];
        }
    }
}
@end
